﻿package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.CardInfoListAdapter;
import com.kuanter.kuanterauto.cache.ObjectCache;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.umeng.analytics.MobclickAgent;
///import com.kuanter.kuanterauto.MyKuanterConsumerActivity.ConsumerLogListClickListener;

public class MyKuanterCardActivity extends WuyangBaseActivity {
	private TextView tvHeaderTitle ;
	private Button btnHeaderLeft ;
	private Button btnHeaderRight ;
	private ImageView include_image_and_2_tv_IV;
	private TextView include_image_and_2_tv_TVtitle;
	private String userId;
	private List<CardInfo> cardInfoList;
	private Intent intent = new Intent();
	private int cardInfoIndex;
Handler handler = new Handler() {

		private ListView lv_cardInfoList;

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
//				String result = msg.getData().getString("result");
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
															// 字段值是否为false
							com.alibaba.fastjson.JSONObject jsonObjectResult = JSON
									.parseObject(result);
							String JsonStringUserInfo = jsonObjectResult
									.getString("cardInfoList");
							cardInfoList = JSON.parseArray(
									JsonStringUserInfo, CardInfo.class);
							// 将cardInfoList缓存
							ObjectCache.set("cardInfoList", cardInfoList);
							
							lv_cardInfoList = (ListView) MyKuanterCardActivity.this.findViewById(R.id.lv_cardInfoList);
							
							lv_cardInfoList.setAdapter(new CardInfoListAdapter(cardInfoList,
									MyKuanterCardActivity.this));
							lv_cardInfoList
									.setOnItemClickListener(new CardInfoListClickListener());
							
							finishProgress();

						} else {
							finishProgress();
							// 显示返回数据中 errorMsg 字段值
							showToast(StringUtil.geterrorMsg(result));
						}
					} catch (JSONException e) {
						finishProgress();
						showToast("登录失败");
						e.printStackTrace();
					}
				} else {
					finishProgress();
					showToast("登录失败,网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
private DataLoader dataLoader;
private View reissue_card_goto;

public void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
}
public void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.my_kuanter_card) ;
		super.onCreate(savedInstanceState);
		//初始化控件
				this.initHeaderControlView();
				this.setHeaderControlViewText();
				this.initBodyControlView();
				this.setBodyControlViewText();
				this.registerListener();
		//下载数据
		dataLoader = new DataLoader(this);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//从SharedPreferences本地化数据中区账号信息
		SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
		userId = sp.getString("id", "000") ;
		params.add(new BasicNameValuePair("cardInfo.userId", userId));
		dataLoader.DisplayData(MainConstants.HOST
				+ HttpConfigAPI.QUERY_CARD, params, handler, "", false);
		
	}
	
	
	@Override
	protected void initHeaderControlView() {
		tvHeaderTitle = (TextView)this.findViewById(R.id.header_title);
		btnHeaderLeft = (Button)this.findViewById(R.id.header_left) ;
		btnHeaderRight = (Button)this.findViewById(R.id.header_right) ;
	}

	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		//设置inlude布局文件的文本
		super.setHeaderTitleText(tvHeaderTitle, "卡与账户");
		super.setHeaderLeftText( btnHeaderLeft, "返回");
		super.setHeaderRightText(btnHeaderRight, null);
	}


	@Override
	protected void initBodyControlView() {
		include_image_and_2_tv_IV = (ImageView)this.findViewById(R.id.include_image_and_2_tv_IV);
		include_image_and_2_tv_TVtitle = (TextView)this.findViewById(R.id.include_image_and_2_tv_TVtitle);
		reissue_card_goto = this.findViewById(R.id.reissue_card_goto);
	}


	@Override
	protected void setBodyControlViewText() {
		include_image_and_2_tv_IV.setImageResource(R.drawable.wallet);
		include_image_and_2_tv_TVtitle.setText("补卡电话");
	}


	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		btnHeaderLeft.setOnClickListener(this);
		reissue_card_goto.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;
		case R.id.reissue_card_goto:
			//模拟打电话
			//showToast("模拟打电话");
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("4006666377");
			builder.setPositiveButton("呼叫",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface arg0,
								int arg1) {
							call("4006666377");
						}
					});

			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0,
								int arg1) {
						}
					});

			builder.create().show();
			break;
		default:
			break;
		}
	}


	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		
	}
	
	private class CardInfoListClickListener implements AdapterView.OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
				intent.setClass(MyKuanterCardActivity.this, MyKuanterCardDetailActivity.class);
				cardInfoIndex = position;
				intent.putExtra("cardInfoIndex", cardInfoIndex);
				startActivity(intent);
		}
		
	}

}
